<template>
	<!-- 智能组卷 -->
	<div class="intelligent-test-pape">
        <div class="manual-test-paper-box">
            <h3><span>智能组卷 <i>></i></span> 创建试卷</h3>
            <div class="manual-test-paper-cont">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="试卷范围"  class="manual-test-scope" required>
                        <testArea ref="testArea" required></testArea>
                    </el-form-item>

                    <el-form-item label="试卷名称" prop="name" class="manual-test-paper-name">
                       <el-input v-model="ruleForm.name" placeholder="输入试卷名称，不超过20字" maxlength="20"></el-input>
                    </el-form-item>
					
					<el-form-item label="试卷类型" prop="type">
					    <el-radio-group v-model="ruleForm.type">
					       <template v-for="item of getTestTypeList">
						  		<el-radio :key="item.id" :label="item.id">{{item.name}}</el-radio>
						  	</template>
					    </el-radio-group>
					</el-form-item>

					<el-form-item label="建议时长" prop="duration" class="duration">
					    <el-input-number v-model="ruleForm.duration" controls-position="right"  :step="1" step-strictly :min="1" :max="500"></el-input-number><span>分钟</span>
					</el-form-item>

					<el-form-item label="组卷形式" prop="shape">
					    <el-radio-group v-model="ruleForm.shape">
					        <el-radio label="1">章节组卷</el-radio>
					        <el-radio label="2">知识点组卷</el-radio>
					    </el-radio-group>
					</el-form-item>

                    <el-form-item  label-width="0">
                        <el-button type="primary" @click="submitForm('ruleForm')">配置考点</el-button>
                        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
                    </el-form-item>
                </el-form>
            </div>
        </div>
	</div>
</template>


<script>
    import testArea  from  './test-area';
	export default {
        components: {
            testArea
        },        
	    data() {
	        return {
	            ruleForm: {
                    grade:'',
                    subject:'',
                    edition:'',
                    material:'',
	                name: '',      /*试卷名称*/
	                type: '',		/*试卷类型*/
	                duration: 100,	/*建议时长*/
	                shape: '',		/*组卷形式*/
	            },
	            getTestTypeList: [],
	            rules: {
	                name: [
	                    { required: true, message: '请输入试卷名称', trigger: 'blur' },
	                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
	                ],
	                type: [
	                    { required: true, message: '请选试卷类型', trigger: 'change' }
	                ],
	                duration: [
	                    { required: true, message: '请输入建议时长', trigger: 'change' }
	                ],
	                shape: [
	                    {  required: true, message: '请选组卷形式', trigger: 'change' }
	                ],
	            }
	        };
	    },
	   	created(){
	   		sessionStorage.removeItem('check')
	   		sessionStorage.removeItem('check2')
	   		sessionStorage.removeItem('check3')
	   		this.getTestType()
	   	},
	    methods: {
	    	/**
	    	 * 请求试卷类型数据
	    	 * @DateTime 2019-07-25T14:09:13+0800
	    	 * @return   {[type]}                 [description]
	    	 */
	    	getTestType(){

	    		let success = (res=>{
	    			this.getTestTypeList = res.data;
	    			// if (this.getTestTypeList.length > 0) {
	    			// 	this.getTestTypeList.unshift({id:0, name: '全部'})
	    			// }
	    		});

	    		let params = {status: 1};

	    		this.$ajax({
	    			url: 'api/newtest/get_test_type',
	    			method: 'get',
	    			params: params,
	    			func: { success: success}
	    		})
	    	},
	        submitForm(formName) {
                // 先验证试卷范围里的几个select框的值是否选择
                this.$refs.testArea.valid();
                let noSelect = Object.values(this.$refs.testArea.ruleForm).includes('');
                if(noSelect)return;
                
                this.ruleForm.grade = this.$refs.testArea.ruleForm.grade;
                this.ruleForm.subject = this.$refs.testArea.ruleForm.subject;
                this.ruleForm.edition = this.$refs.testArea.ruleForm.edition;
                this.ruleForm.material = this.$refs.testArea.ruleForm.material;

                // 验证其他选项，通过则提交
	            this.$refs[formName].validate((valid) => {
	                if (valid) {
	                	let success = (res =>{
	                		this.$message.success(res.msg);
	                		sessionStorage.setItem('intelligentObj', JSON.stringify(this.ruleForm))
	                		this.$router.push({path: '/test/intelligent-node', query: {id: res.data, auth_type: this.ruleForm.shape}})
	                	});
	                	let params = {
	                		create_id: this.$g.user_id,
	                		name: this.ruleForm.name,
	                		grade_id: this.$refs.testArea.ruleForm.grade,
	                		subject_id: this.$refs.testArea.ruleForm.subject,
	                		version_id: this.$refs.testArea.ruleForm.edition,
	                		course_id: this.$refs.testArea.ruleForm.material,
	                		source_id: this.ruleForm.type,
	                		answer_time: this.ruleForm.duration,
	                		auth_type: this.ruleForm.shape,
	                		authority: 0,
	                		status: 0,
	                	};
	                	this.$ajax.post('api/newtest/add_test', params, {func: {success: success}});
	                } else {
	                    console.log('error submit!!');
	                    return false;
	                }
	            });
	        },
	        resetForm(formName) {
                this.$refs.testArea.reset();//试卷范围几个select重置验证
	            this.$refs[formName].resetFields();
	        }
	    }
	}
</script>